package jay.home.bet.test;

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import jay.home.bet.dao.ibatis.SqlMapOddsDao;
import jay.home.bet.dao.ibatis.SqlMapOrderDao;
import jay.home.bet.domain.Banquanchang;
import jay.home.bet.domain.Bodan;
import jay.home.bet.domain.Comp;
import jay.home.bet.domain.Danshuang;
import jay.home.bet.domain.Daxiao;
import jay.home.bet.domain.Duying;
import jay.home.bet.domain.Event;
import jay.home.bet.domain.League;
import jay.home.bet.domain.Order;
import jay.home.bet.domain.Rangqiu;
import jay.home.bet.domain.Ruqiushu;
import jay.home.bet.domain.Team;
import jay.home.bet.domain.logic.BasicService;
import jay.home.bet.domain.logic.BetFacade;
import jay.home.bet.domain.logic.OddsService;
import jay.home.bet.domain.logic.OrderService;
import jay.home.bet.misc.Util;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTestZhang {
	static BetFacade bet;
	static OrderService orderService;
	static BeanFactory factory;
	static BasicService basicService;
	static OddsService oddsService;

	@BeforeClass
	public static void setUpBeforeClass()  throws Exception
	{
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml", "dataAccessContext-local.xml"});
		factory = (BeanFactory) context;
//		bet = (BetFacade)factory.getBean("bet");
		orderService = (OrderService)factory.getBean("orderService");
		oddsService = (OddsService)factory.getBean("oddsService");
		basicService = (BasicService)factory.getBean("basicService");
	}
	public void testInsertOdds() throws Exception
	{
		Event event = new Event();
		event.setId(Util.uuid());
		event.setType("danshi");
		
		event.setComp(new Comp());
		event.getComp().setLeague(new League());
		event.getComp().setHome(new Team());
		event.getComp().setRoad(new Team());
		
		event.setDaxiao(new Daxiao());
		event.setRangqiu(new Rangqiu());
		event.setDuying(new Duying());
		event.setDanshuang(new Danshuang());
		event.setBanquanchang(new Banquanchang());
		event.setBodan(new Bodan());
		event.setRuqiushu(new Ruqiushu());
		event.setDaxiaoHalf(new Daxiao());
		event.setRangqiuHalf(new Rangqiu());
		event.setDuyingHalf(new Duying());
		
		
		event.getComp().setId(Util.uuid());
		event.getComp().getLeague().setId("1");
		event.getComp().setTime(Util.string2Date("2007-03-12 12:30:45"));
		event.getComp().getHome().setId("2");
		event.getComp().getRoad().setId("8"); 
		
		event.getDaxiao().setHome(Math.random());
		event.getDaxiao().setRoad(Math.random());
		event.getDaxiao().setPankou("daxiaoHalf.pankou");
		event.getRangqiu().setHome(Math.random());
		event.getRangqiu().setRoad(Math.random());
		event.getRangqiu().setPankou("rangqiu.pankou");
		event.getRangqiu().setWho2who("rangqiu.who2who");
		event.getDuying().setWin(Math.random());
		event.getDuying().setLose(Math.random());
		event.getDuying().setDraw(Math.random());
		event.getDanshuang().setOdd(Math.random());
		event.getDanshuang().setEven(Math.random());
		event.getBanquanchang().setHr(Math.random());
		event.getBanquanchang().setHh(Math.random());
		event.getBanquanchang().setHd(Math.random());
		event.getBanquanchang().setRr(Math.random());
		event.getBanquanchang().setRh(Math.random());
		event.getBanquanchang().setRd(Math.random());
		event.getBanquanchang().setDd(Math.random());
		event.getBanquanchang().setDh(Math.random());
		event.getBanquanchang().setDr(Math.random());
		event.getBodan().setS00(Math.random());
		event.getBodan().setS01(Math.random());
		event.getBodan().setS02(Math.random());
		event.getBodan().setS03(Math.random());
		event.getBodan().setS04(Math.random());
		event.getBodan().setS10(Math.random());
		event.getBodan().setS11(Math.random());
		event.getBodan().setS12(Math.random());
		event.getBodan().setS13(Math.random());
		event.getBodan().setS14(Math.random());
		event.getBodan().setS20(Math.random());
		event.getBodan().setS21(Math.random());
		event.getBodan().setS22(Math.random());
		event.getBodan().setS23(Math.random());
		event.getBodan().setS24(Math.random());
		event.getBodan().setS30(Math.random());
		event.getBodan().setS31(Math.random());
		event.getBodan().setS32(Math.random());
		event.getBodan().setS33(Math.random());
		event.getBodan().setS34(Math.random());
		event.getBodan().setS40(Math.random());
		event.getBodan().setS41(Math.random());
		event.getBodan().setS42(Math.random());
		event.getBodan().setS43(Math.random());
		event.getBodan().setS44(Math.random());
		event.getRuqiushu().setS01(Math.random());
		event.getRuqiushu().setS23(Math.random());
		event.getRuqiushu().setS46(Math.random());
		event.getRuqiushu().setSup7(Math.random());
		event.getDaxiaoHalf().setHome(Math.random());
		event.getDaxiaoHalf().setRoad(Math.random());
		event.getDaxiaoHalf().setPankou("daxiaoHalf.pankou");
		event.getRangqiuHalf().setHome(Math.random());
		event.getRangqiuHalf().setRoad(Math.random());
		event.getRangqiuHalf().setPankou("rangqiuHalf.pankou");
		event.getRangqiuHalf().setWho2who("rangqiuHalf.who2who");
		event.getDuyingHalf().setWin(Math.random());
		event.getDuyingHalf().setLose(Math.random());
		event.getDuyingHalf().setDraw(Math.random());
		
		oddsService.insertEvent(event);
		basicService.insertComp(event.getComp());
	}
	

	public  void testGetOdds() throws Exception
	{
		Event event = oddsService.getEvent("00c9a406-a9d0-4e16-9500-1e2b0ddb8e89");
		System.out.println(event);
		
		System.out.println("aaa");
	}
	
	public void testGetComp()
	{
		SqlMapOddsDao dao = (SqlMapOddsDao) factory.getBean("oddsDao");
		Comp comp = dao.getCompByDetail("10" , "7" , "2007-01-12 00:30:45");
		System.out.println( comp.getId() );
		System.out.println( comp.getHome().getId() );
	}

	
	public void testDate()
	{
		Date date = new Date();
		
		date.setHours(23);
		date.setMinutes(59);
		date.setSeconds(59);
		System.out.println(Util.date2String(date));
	}
	
	public void testDate2()
	{
		Date date = new Date();

		DateFormat shortDateFormat =
		DateFormat.getDateTimeInstance(
		DateFormat.SHORT,
		DateFormat.SHORT);

		DateFormat mediumDateFormat =
		DateFormat.getDateTimeInstance(
		DateFormat.MEDIUM,
		DateFormat.MEDIUM);

		DateFormat longDateFormat =
		DateFormat.getDateTimeInstance(
		DateFormat.LONG,
		DateFormat.LONG);

		DateFormat fullDateFormat =
		DateFormat.getDateTimeInstance(
		DateFormat.FULL,
		DateFormat.FULL);

		System.out.println(shortDateFormat.format(date));
		System.out.println(mediumDateFormat.format(date));
		System.out.println(longDateFormat.format(date));
		System.out.println(fullDateFormat.format(date));
	}
	
	@Test
	public void testUpdateData() throws Exception 
	{
		boolean b = Pattern.matches( "[a-zA-Z0-9]{6,18}", "12as111" );
		assertTrue(b);
	}
	
	@Test
	public void testGetUn() throws Exception
	{
		String encoding = System.getProperty("file.encoding");
		System.out.println(encoding);
	}
	
	
	public void testGetUnInfo() throws Exception
	{
		String str = " 中文测试，这是内部硬编码的串"+" test english character";
		String strin= "";
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in,"gb2312")); //设置输入接口按中文编码
		BufferedWriter stdout = new BufferedWriter(new OutputStreamWriter(System.out,"gb2312")); //设置输出接口按中文编码
		stdout.write("请输入:");
		stdout.flush();
		strin = stdin.readLine();
		stdout.write("这是从用户输入的串："+strin);
		stdout.write(str);
		stdout.flush();
	}
	
/*	@Test
	public void testGetSatistic() throws Exception
	{
		SqlMapOrderDao dao = (SqlMapOrderDao)factory.getBean("orderDao");
		List<Order> list = dao.getSatistic(1, 20);
		
		for ( Order order : list )
		{
			System.out.println("username : " +  order.getUsername());
			System.out.println("userCount : " +  order.getAmount());
			System.out.println("sumAmount : " +  order.getItems());
			System.out.println("result : " +  order.getCat());
		}
		
	}*/
}
